package cn.xz.ui3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ArrayList<Integer> myNum1 = new ArrayList<Integer>(); //当前选择的数字
    private int myNum = 0; //当前选择运算 0-加，1-减，2-乘，3-除
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //显示数字
        TextView tvTxt = findViewById(R.id.tv_txt);
        //数字按钮
        TextView tv0 = findViewById(R.id.tv0);
        TextView tv1 = findViewById(R.id.tv1);
        TextView tv2 = findViewById(R.id.tv2);
        TextView tv3 = findViewById(R.id.tv3);
        TextView tv4 = findViewById(R.id.tv4);
        TextView tv5 = findViewById(R.id.tv5);
        TextView tv6 = findViewById(R.id.tv6);
        TextView tv7 = findViewById(R.id.tv7);
        TextView tv8 = findViewById(R.id.tv8);
        TextView tv9 = findViewById(R.id.tv9);
        //加
        TextView tvjia = findViewById(R.id.tvjia);
        //减
        TextView tvjian = findViewById(R.id.tvjian);
        //乘法
        TextView tvc = findViewById(R.id.tvc);
        //除
        TextView tvchu = findViewById(R.id.tvchu);
        //登号
        TextView tvd = findViewById(R.id.tvd);
        //清除
        TextView tv_mc = findViewById(R.id.tv_mc);
        tv_mc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myNum = 0;
                myNum1.clear();
                tvTxt.setText("0");
            }
        });

        tv0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("0");
                myNum1.add(0);
            }
        });
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("1");
                myNum1.add(1);
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("2");
                myNum1.add(2);
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("3");
                myNum1.add(3);
            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("4");
                myNum1.add(4);
            }
        });
        tv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("5");
                myNum1.add(5);
            }
        });
        tv6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("6");
                myNum1.add(6);
            }
        });
        tv7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("7");
                myNum1.add(7);
            }
        });
        tv8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("8");
                myNum1.add(8);
            }
        });
        tv9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTxt.setText("9");
                myNum1.add(9);
            }
        });
        tvjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myNum = 0;
                 String s = "";
                 for (Integer in : myNum1){
                     s = s + in + "+";
                 }
                 tvTxt.setText(s);
            }
        });
        tvjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myNum = 1;
                String s = "";
                for (Integer in : myNum1){
                    s = s + in + "-";
                }
                tvTxt.setText(s);
            }
        });
        tvc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myNum = 2;
                String s = "";
                for (Integer in : myNum1){
                    s = s + in + "x";
                }
                tvTxt.setText(s);
            }
        });
        tvchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myNum = 3;
                String s = "";
                for (Integer in : myNum1){
                    s = s + in + "/";
                }
                tvTxt.setText(s);
            }
        });
        tvd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long s = 0;
                if (myNum == 0){
                    for (Integer in : myNum1){
                        s = s + in;
                    }
                }
                if (myNum == 1){
                    for (Integer in : myNum1){
                        s = s - in;
                    }
                }
                if (myNum == 2){
                    s= 1;
                    for (Integer in : myNum1){
                        s = s * in;
                    }
                }
                if (myNum == 3){
                    s = 1;
                    for (Integer in : myNum1){
                        s = s / in;
                    }
                }

                tvTxt.setText(s + "");
            }
        });

    }
}